package com.card.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.card.R;
import com.card.db.entity.User;
import com.card.db.service.UserService;

public class LoginActivity extends AppCompatActivity {
    private UserService userService;

    /**
     * 学生卡号
     **/
    private String telephone;
    /**
     * 密码
     **/
    private String password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_actiity);
        //初始化userService
        userService = new UserService(this);
        userService.queryAllUser();
    }

    /**
     * 跳转到主界面
     *
     * @param view
     */
    public void loginToMain(View view) {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }

    /**
     * 登录 校验密码和学生证
     *
     * @param view
     */
    public void login(View view) {
        //初始化数据
        initData();
            Log.e("login_username", "login: " + telephone);
            Log.e("login_password", "login: " + password);
        User user = userService.login(telephone, password);
        System.out.println(user);
        if (user != null) {
            //放数据
            Intent intent = new Intent(this, FunctionActivity.class);
            Bundle bundle = new Bundle();
            bundle.putInt("id",user.getId());
            bundle.putString("telephone",user.getTelephone());
            bundle.putString("password",user.getPassword());
            intent.putExtras(bundle);
            //存在该用户就跳转界面
            startActivity(intent);
        }
        else {
            Toast.makeText(this,"用户名或密码错误",Toast.LENGTH_LONG).show();
            EditText passWordEditText = findViewById(R.id.password);
            passWordEditText.setText("");
        }
    }

    /**
     * 获取数据 学生卡号和密码
     */
    public void initData() {
        //获取组件
        EditText passWordEditText = findViewById(R.id.password);
        EditText schoolCardEditText = findViewById(R.id.schoolCardNo);
        //获取用户名和密码
        password = passWordEditText.getText().toString();
        telephone = schoolCardEditText.getText().toString();
    }
}
